package com.genesis.web.controller.member;

import com.genesis.mall.common.Result;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.MemberReadHistory;
import com.genesis.mall.service.IMemberReadHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author: HShuai
 * @Date: 2022/08/03/21:59星期三
 * @Description:  用户浏览记录
 */

@RestController
@RequestMapping("/member/readHistory")
public class MemberReadHistoryController {

    @Autowired
    private IMemberReadHistoryService memberReadHistoryService;

    /**
     * 清空浏览记录
     * @param request
     * @return
     */
    @PostMapping("/clear")
    public Result clear(HttpServletRequest request){
        memberReadHistoryService.clearById(request);
        return Result.success();
    }

    /**
     * 创建浏览记录
     * @param memberReadHistory
     * @return
     */
    @PostMapping("/create")
    public Result create(@RequestBody MemberReadHistory memberReadHistory){
        memberReadHistory.setCreateTime(LocalDateTime.now());
        memberReadHistoryService.save(memberReadHistory);
        return Result.success();
    }

    /**
     * 删除浏览记录
     * @param ids
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public  Result delete(@RequestParam Long ids,HttpServletRequest request){
        memberReadHistoryService.deleteById(ids,request);
        return Result.success();
    }

    /**
     * 分页获取浏览记录
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public Result<PageDto<MemberReadHistory>> pageDtoResult (HttpServletRequest request,Integer pageNum,Integer pageSize){
        return Result.success(memberReadHistoryService.pageList(request,pageNum,pageSize));

    }

}
